package com.mf.service.impl;

import com.mf.entity.GoodsAttribute;
import com.mf.repository.GoodsAttributeReponsitory;
import com.mf.service.GoodsAttributeService;
import com.mf.util.StringUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.List;

@Service("goodsAttributeService")
public class GoodsAttributeImpl implements GoodsAttributeService {

    @Resource
    private GoodsAttributeReponsitory goodsAttributeReponsitory;

    @Override
    public List<GoodsAttribute> list(GoodsAttribute goodsAttribute, Integer page, Integer pageSize, Sort.Direction direction, String... properties) {
        Pageable pageable = new PageRequest(page - 1, pageSize);

        Page<GoodsAttribute> goodsAttributesPage = goodsAttributeReponsitory.findAll(new Specification<GoodsAttribute>() {
            @Override
            public Predicate toPredicate(Root<GoodsAttribute> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate predicate = criteriaBuilder.conjunction();

                if (goodsAttribute != null) {
//                    if (goodsAttribute.getAttr_id() != null) {
//                        predicate.getExpressions().add(criteriaBuilder.like(root.get("id"), "%" + goodsAttribute.getAttr_id() + "%"));
//                    }

                    if (StringUtil.isNotEmpty(goodsAttribute.getAttrName())) {
                        predicate.getExpressions().add(criteriaBuilder.like(root.get("attrName"), "%" + goodsAttribute.getAttrName().trim() + "%"));
                    }

                    if (goodsAttribute.getId() != null) {
                        predicate.getExpressions().add(criteriaBuilder.equal(root.get("id"), goodsAttribute.getId()));
                    }

                    if (goodsAttribute.getGoodsMold() != null && goodsAttribute.getGoodsMold().getId() != null) {
                        predicate.getExpressions().add(criteriaBuilder.like(root.get("goodsMold").get("id"), "%" + goodsAttribute.getGoodsMold().getId() + "%"));
                    }

                    if (goodsAttribute.getAttrIndex() != null) {
                        predicate.getExpressions().add(criteriaBuilder.like(root.get("attrIndex"), "%" + goodsAttribute.getAttrIndex() + "%"));
                    }

                    if (goodsAttribute.getAttrType() != null) {
                        predicate.getExpressions().add(criteriaBuilder.like(root.get("attrType"), "%" + goodsAttribute.getAttrType() + "%"));
                    }

                    if (goodsAttribute.getAttrInputType() != null) {
                        predicate.getExpressions().add(criteriaBuilder.like(root.get("attrInputType"), "%" + goodsAttribute.getAttrInputType() + "%"));
                    }

                    if (StringUtil.isNotEmpty(goodsAttribute.getAttrValues())) {
                        predicate.getExpressions().add(criteriaBuilder.like(root.get("attrValues"), "%" + goodsAttribute.getAttrValues() + "%"));
                    }

                }
                return predicate;
            }
        }, pageable);
        return goodsAttributesPage.getContent();
    }

    @Override
    public Long getCount(GoodsAttribute goodsAttribute) {
        Long count = goodsAttributeReponsitory.count(new Specification<GoodsAttribute>() {
            @Override
            public Predicate toPredicate(Root<GoodsAttribute> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                Predicate predicate = criteriaBuilder.conjunction();
                if (goodsAttribute != null) {
                    if (goodsAttribute.getId() != null) {
                        predicate.getExpressions().add(criteriaBuilder.equal(root.get("id"), goodsAttribute.getId()));
                    }

                    if (StringUtil.isNotEmpty(goodsAttribute.getAttrName())) {
                        predicate.getExpressions().add(criteriaBuilder.like(root.get("attrName"), "%" + goodsAttribute.getAttrName().trim() + "%"));
                    }

//                    if (goodsAttribute.getAttr_id() != null) {
//                        predicate.getExpressions().add(criteriaBuilder.like(root.get("id"), "%" + goodsAttribute.getAttr_id() + "%"));
//                    }

                    if (goodsAttribute.getGoodsMold() != null && goodsAttribute.getGoodsMold().getId() != null) {
                        predicate.getExpressions().add(criteriaBuilder.equal(root.get("goodsMold").get("id"), goodsAttribute.getGoodsMold().getId()));
                    }

                    if (goodsAttribute.getAttrIndex() != null) {
                        predicate.getExpressions().add(criteriaBuilder.equal(root.get("attrIndex"), goodsAttribute.getAttrIndex()));
                    }

                    if (goodsAttribute.getAttrType() != null) {
                        predicate.getExpressions().add(criteriaBuilder.equal(root.get("attrType"), goodsAttribute.getAttrType()));
                    }

                    if (goodsAttribute.getAttrInputType() != null) {
                        predicate.getExpressions().add(criteriaBuilder.equal(root.get("attrInputType"), goodsAttribute.getAttrInputType()));
                    }

                    if (StringUtil.isNotEmpty(goodsAttribute.getAttrValues())) {
                        predicate.getExpressions().add(criteriaBuilder.like(root.get("attrValues"), "%" + goodsAttribute.getAttrValues() + "%"));
                    }
                }
                return predicate;
            }
        });
        return count;
    }

    @Override
    public void delete(Integer id) {
        goodsAttributeReponsitory.delete(id);
    }

    @Override
    public void save(GoodsAttribute goodsAttribute) {
        goodsAttributeReponsitory.save(goodsAttribute);
    }

}
